package com.ykt.model;

import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;


public class Service extends BaseModel<Service> {
    private static final long serialVersionUID = -3857562245294137140L;


    public static final String order_id = "order_id";
    public static final String user_id = "user_id";
    public static final String service_type = "service_type";
    public static final String summary = "summary";
    public static final String reasons = "reasons";
    public static final String pic = "pic";
    public static final String status = "status";

    public enum ServiceType {
        /**
         * 换货
         */
        EXCHANGE,
        /**
         * 退货退款
         */
        BACK
    }

    public enum Status {
        /**
         * 换货
         */
        HAS_EXCHANGE, /**
         * 退货退款
         */
        HAS_BACK, /**
         * 拒绝
         */
        HAS_REFUSE
    }

    public static final Service dao = new Service();


    @Override
    public Page<Service> paginate(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT s.order_id, u.name userName, u.phone userPhone, sh.name shopName, "
                + "s.id, s.service_type serviceType, s.summary, s.status, s.reasons, s.pic, s.created_time createdTime ";

        String exceptSelect = " FROM `service` s "
                + " LEFT JOIN `user` u ON s.user_id = u.id "
                + " LEFT JOIN `order` o ON s.order_id = o.id "
                + " LEFT JOIN `shop` sh ON o.shop_id=sh.id ";

        String sqlOrderBy = " ORDER BY s.id DESC ";

        paramMap.put(OB, sqlOrderBy);

        return this.paginate(select, exceptSelect, pageNumber, rows, paramMap);
    }

    // 查询指定最新的一条服务详情
    public Service findLastService(int orderId) {
        String sql = "SELECT * FROM service WHERE order_id=? ORDER BY created_time DESC LIMIT 1";
        return this.findFirst(sql, orderId);
    }
    
    // 查询订单对应的所有服务
    public List<Service> findByOrderId(int orderId) {
        String sql = "SELECT * FROM service WHERE order_id=? ORDER BY id DESC";
        return this.find(sql, orderId);
    }
}
